package sample.widgets;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;

import com.lambda.widgets.FitTextView;

public class MainActivity extends AppCompatActivity implements Handler.Callback {
    private final String[] words = {"Return", "Application", "Matt", "Google", "Something", "Fucshina", "Terminal", "Link", "God", "Switcher", "Calculus"};

    private FitTextView mFitTv;
    private int len = 1;

    private Handler mHandler = new Handler(this);


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mFitTv = findViewById(R.id.fit_tv);
    }

    @Override
    protected void onResume() {
        super.onResume();
        mHandler.sendEmptyMessageDelayed(99, 666);
    }

    @Override
    protected void onPause() {
        mHandler.removeMessages(99);
        super.onPause();
    }

    @Override
    public boolean handleMessage(Message msg) {
        setFitTvText();
        mHandler.sendEmptyMessageDelayed(99, 888);
        return true;
    }

    private void setFitTvText() {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < len; i++) {
            sb.append(words[i]).append(' ');
        }
        sb.setLength(sb.length() - 1);
        mFitTv.setText(sb.toString());
        ++len;
        if (len >= words.length) {
            len = 1;
        }
    }
}
